<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户信息</title>
    <style>
        html,
        body {
            margin: 0;
            padding: 0;
            background-color: #f2f4f8;
        }

        * {
            box-sizing: border-box;
        }

        .box {
            width: 1000px;
            margin: 0 auto;
        }

        .row {
            display: flex;
            flex-direction: row;
            justify-content: flex-start;
            align-items: center;
            padding: 30px 20px;
            border-bottom: 1px solid #e2e2e2;

        }

        .label {
            width: 200px;
            font-size: 14px;
            color: #999999;
        }

        .content {
            font-size: 16px;
            color: #333333;
        }

        #avatar {
            margin-right: 20px;
            width: 80px;
            height: 80px;
            background-color: #cccccc;
            border-radius: 50%;
            overflow: hidden;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="row">
            <div class="label">手机号</div>
            <div class="content">
                <span id="phone"></span>
            </div>
        </div>
        <div class="row">
            <div class="label">头像</div>
            <div class="content">
                <img id="avatar" src="" alt="">
                <input id="file" type="file" accept="*/image">
            </div>
        </div>
    </div>

    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script>
        $(function () {
            function fetchUserInfo () {
                $.ajax({
                    url: "/api/user/userInfo",
                    method: "GET",
                    headers: {
                        token: localStorage.getItem("token")
                    },
                    success: function(res) {
                        if(res.code === 0) {
                            var userInfo = res.data.data;
                            $("#phone").html(userInfo.phone)
                            $("#avatar").attr("src", userInfo.avatar)
                        }else {
                            alert(res.msg)
                        }
                    }
                })
            }

            fetchUserInfo();
            $("#file").on("change", function(e) {
                var formData = new FormData();
                var files = e.target.files;
                console.log(files);
                if(files && files.length > 0) {
                    formData.append('file', files[0])
                    $.ajax({
                        url: "/api/user/avatarUpload",
                        method: "POST",
                        data: formData,
                        contentType: false,
                        processData: false,
                        headers: {
                            token: localStorage.getItem("token")
                        },
                        success: function (res) {
                            if(res.code === 0) {
                                var userInfo = res.data.data;
                                $("#avatar").attr("src", userInfo.avatar)
                            }else {
                                alert(res.msg)
                            }
                        }
                    })
                }
            })
            
        })
    </script>
</body>

</html>